﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Milan.Deploy
{
	/// <summary>
	/// A scanner is an internal service which can be started and stopped, sometimes paused and restarted.
	/// Results can be passed into and out of a scanner.  Scanners are the basic building blocks of a Milan Application.
	/// </summary>
	public abstract class Scanner
	{
		public Scanner(Application application)
		{
			m_application = application;
		}
		/// <summary>
		/// Gets the current application object
		/// </summary>
		public Application CurrentApplication
		{
			get
			{
				return m_application;
			}
		}
		private Application m_application;
		/// <summary>
		/// Every internal service starts with a call to Start().  The application can block, however,
		/// it is recommended that a call to Stop works to shut the application down at any time.
		/// </summary>
		/// <param name="application"></param>
		/// <returns></returns>
		public abstract bool OnStart(string[] args);
		/// <summary>
		/// Every application must be able to cleanly terminate with a call to Stop() at any time.
		/// </summary>
		public abstract void OnStop();
		/// <summary>
		/// Optional pausing of application
		/// </summary>
		protected virtual void OnPause()
		{
		}
		/// <summary>
		/// Optional continuation of application
		/// </summary>
		protected virtual void OnContinue()
		{
		}
		/// <summary>
		/// Event which occurs when system is shutting down.
		/// </summary>
		protected virtual void OnShutdown()
		{
		}
		/// <summary>
		/// Event which occurs when a power event occurs
		/// </summary>
		protected virtual void OnPowerEvent()
		{
		}
		public virtual bool IsBlocking { get { return false; } }
		public bool IsRunning { get; set; }
		public virtual bool ReturnsExitCode { get { return false; } }
		public int ExitCode { get; set; }
		public virtual void Stop()
		{
			if (IsRunning)
			{
				OnStop();
				IsRunning = false;
			}
		}
	}
}
